﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Threading;

namespace DataEntry
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnAddMenus_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dlgChooseMenuFolder = new System.Windows.Forms.FolderBrowserDialog();
            dlgChooseMenuFolder.ShowDialog();
            string menuPath = dlgChooseMenuFolder.SelectedPath;
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(menuPath);


                    foreach (var menuDir in dirInfo.GetDirectories())
                    {
                        //Add a new Menu
                        Menus newMenu = new Menus();
                        newMenu.MenuName = Capitalize(menuDir.Name);
                        newMenu.Add();

                        //Add MenuItems only if the menu was succesfully added!

                        if (newMenu.MenuID >= 0)
                        {
                            //Get a list of pictures
                            FileInfo[] arrPictures = menuDir.GetFiles("*.gif");
                            //A dictionary to save file name as a key, file path as a value
                            Dictionary<string, string> PicturesDic = new Dictionary<string, string>();
                            //Loop to save pictures information in the PictureDic
                            foreach (var pic in arrPictures)
                            {
                                StringBuilder picName = new StringBuilder();
                                var chars = from c in pic.Name
                                            select c;
                                foreach (var c in chars)
                                {
                                    if (c != '.')
                                    {
                                        picName.Append(c);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                PicturesDic.Add(picName.ToString(), pic.FullName);
                            }

                            //Get a list of txt files
                            FileInfo[] arrNames = menuDir.GetFiles("*.txt");
                            //A variable for the real text file that has the MenuItems info
                            FileInfo MyTextFile = new FileInfo(arrNames[0].FullName);
                            //Running a loop to know the real text file based on the Directory Name
                            foreach (var txtFile in arrNames)
                            {
                                StringBuilder txtName = new StringBuilder();
                                var chars = from c in txtFile.Name
                                            select c;
                                foreach (var c in chars)
                                {
                                    if (c != '.')
                                    {
                                        txtName.Append(c);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (txtName.ToString() == menuDir.Name)
                                {
                                    MyTextFile = txtFile;
                                    break;
                                }
                            }

                            //Open the real text file
                            StreamReader txtStream = MyTextFile.OpenText();
                            
                            while (!txtStream.EndOfStream)
                            {
                                //Getting Name, description and the price from the text file
                                string name = txtStream.ReadLine();
                                string des = txtStream.ReadLine();
                                string price = txtStream.ReadLine();
                                //Initializing new MenuItem
                                MenuItem menuItem = new MenuItem();
                                menuItem.MenuID = newMenu.MenuID;
                                menuItem.MenuItemName = Capitalize(name) ;
                                menuItem.MenuItemDescription = Capitalize(des);
                                menuItem.MenuItemPrice = Decimal.Parse(price);
                                //Setting the Picture property to the full path of the image with the same MenuItem.Name
                                menuItem.Picture = PicturesDic[name];

                                //Add the new MenuItem
                                menuItem.Add();
                            }
                        }
                    }
                    MessageBox.Show("Done adding stuff!");
                }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void btnDeleteMenus_Click(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    DataAccessDataContext obj = new DataAccessDataContext();
                    var deletemenus = from menu in obj.Menus
                                      select menu;
                    foreach (var _menu in deletemenus)
                    {
                        _menu.Delete();
                    }
                    MessageBox.Show("Menus are deleted!");
                }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void btnDeleteMenuItems_Click(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    DataAccessDataContext obj = new DataAccessDataContext();
                    var deletemenus = from menu in obj.MenuItems
                                      select menu;
                    foreach (var _menu in deletemenus)
                    {
                        _menu.Delete();
                    }
                    MessageBox.Show("Menu Items are deleted!");
                }));
            thr.IsBackground = true;
            thr.Start();
        }


        public static string Capitalize(string Text)
        {
            char[] chars = Text.ToLowerInvariant().ToCharArray();
            chars[0] = chars[0].ToString().ToUpperInvariant().ToCharArray()[0];
            StringBuilder sb = new StringBuilder();
            foreach (var c in chars)
            {
                sb.Append(c);
            }
            return sb.ToString();
        }
    }
}
